package com.inet.test07;


import java.io.*;
import java.net.Socket;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * @Author weijingchen
 * @Date 2025/6/26 20:38
 * @Version 1.0.0
 */
public class MyRunnable implements Runnable {
    Socket socket;
    List<User> list;

    public MyRunnable(Socket socket, List<User> list) {
        this.socket = socket;
        this.list = list;
    }

    @Override
    public void run() {
        try (BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()))) {
            while (true) {
                String result = br.readLine();
                switch (result) {
                    case "login" -> login(br);
                    case "register" -> register(br);
                }
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    private void register(BufferedReader br) throws IOException {
        System.out.println("用户选择了注册操作");
        String userInfo = br.readLine();
        System.out.println(userInfo);
        String[] userInfoArr = userInfo.split("&");
        String username = userInfoArr[0].split("=")[1];
        String password = userInfoArr[1].split("=")[1];
        List<String> usernames = list.stream().map(User::getUsername).toList();
        if (usernames.contains(username)) {
            writeMessage2Client("2");
        } else {
            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("src/main/resources/f.txt", true)));
            writer.newLine();
            writer.write(username + "=" + password);
            writer.flush();
            writer.close();
            User user = new User(username, password);
            list.add(user);
            writeMessage2Client("1");
        }
    }

    private void login(BufferedReader br) throws IOException {
        System.out.println("用户选择了登录操作");
        String userInfo = br.readLine();
        System.out.println(userInfo);
        String[] userInfoArr = userInfo.split("&");
        String username = userInfoArr[0].split("=")[1];
        String password = userInfoArr[1].split("=")[1];
        List<String> usernames = list.stream().map(User::getUsername).toList();
        if (!usernames.contains(username)) {
            writeMessage2Client("2");
        } else {
            long count = list.stream().filter(user -> user.getUsername().equals(username) && user.getPassword().equals(password)).count();
            if (count == 1) {
                Thread.currentThread().setName(username);
                Server.socketList.add(socket);
                writeMessage2Client("1");
                talk2All(br);
            } else {
                writeMessage2Client("3");
            }

        }
    }

    private void talk2All(BufferedReader br) throws IOException {
        while (true) {
            String message = br.readLine();
            System.out.println(Thread.currentThread().getName() + "说：" + message);
            for (Socket s : Server.socketList) {
                writeMessage2Client(s, Thread.currentThread().getName() + "说：" + message);
            }
        }

    }

    public void writeMessage2Client(String message) throws IOException {
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
        bw.write(message);
        bw.newLine();
        bw.flush();
    }

    public void writeMessage2Client(Socket s, String message) throws IOException {
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
        bw.write(message);
        bw.newLine();
        bw.flush();
    }
}

